package utils

import (
	"github.com/bwmarrin/snowflake"
	"strconv"
	"time"
)

var ServerNode *snowflake.Node

// GenId 生成ID
func GenId() int64 {
	return ServerNode.Generate().Int64()
}

// GenIdStr 生成字符串类型的ID
func GenIdStr() string {
	return strconv.Itoa(int(ServerNode.Generate().Int64()))
}

// InitSnowflake 初始化node节点
// startTime 开始时间
// machineID 设备ID
func InitSnowflake(startTime string, machineID int64) error {

	var st time.Time
	st, err := time.Parse("2006-01-02", startTime)
	if err != nil {
		return err
	}

	snowflake.Epoch = st.UnixNano() / 1000000

	ServerNode, err = snowflake.NewNode(machineID)
	if err != nil {
		return err
	}

	return nil
}
